package day4;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.junit.Test;

import day2.Student;

public class JDBCDemo {
	
	public static void main(String[] args) {
		// lib library bin:binary
		
		//1、加载驱动Class.forName("");  
		try {
			Class.forName("com.mysql.jdbc.Driver");
			//2、获得连接对象Connection
			Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java2020?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
			//3、写sql语句
			String sql = "select id,name,age,gender from student";
			//4、创建Statement
			PreparedStatement statement = connection.prepareStatement(sql);
			//5、执行sql语句
			//     (1) 更新类（更改了表里面数据）：delete/update/insert     executeUpdate()
			//           返回值：int，表示你影响的行数
			//      (2)查询（没有改变表里面数据）:  select                  executeQuery()
			//           返回值：结果集ResultSet
			ResultSet resultSet = statement.executeQuery();
			ArrayList<Student> list = new ArrayList<Student>();
			while (resultSet.next()) {
				// 每遍历一次就是查出数据库表里面一行数据，一列一列取出来，最后封装成一个Student对象
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
			
			//6、关闭连接
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	@Test
	public void test1() {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,name,age,gender from student";
			statement = connection.prepareStatement(sql);
			resultSet = statement.executeQuery();
			ArrayList<Student> list = new ArrayList<Student>();
			while (resultSet.next()) {
				// 每遍历一次就是查出数据库表里面一行数据，一列一列取出来，最后封装成一个Student对象
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接
			JDBCUtil.close(connection, statement, resultSet);
		}
	}
	
	@Test
	public void testSelect() {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		String searchName = "李"; 
		try {
			connection = JDBCUtil.getConnection();
			// String sql = "select id,name,age,gender from student where name like '%张%'";
			String sql = "select id,name,age,gender from student where name like ?";
			statement = connection.prepareStatement(sql);
			// statement.setString(1, "%张%");
			// 豆豆加加
			statement.setString(1, "%" + searchName + "%");
			resultSet = statement.executeQuery();
			System.out.println(statement);
			ArrayList<Student> list = new ArrayList<Student>();
			while (resultSet.next()) {
				// 每遍历一次就是查出数据库表里面一行数据，一列一列取出来，最后封装成一个Student对象
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接
			JDBCUtil.close(connection, statement, resultSet);
		}
	}
	
	@Test
	public void testInsert() {
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "insert into student(name,age,gender) values(?,?,?)";
			statement = connection.prepareStatement(sql);
			// 填上？具体的值
			statement.setString(1, "小张");
			statement.setInt(2, 23);
			statement.setString(3, "女");
			// 返回影响的行数
			int count = statement.executeUpdate();
			System.out.println(statement);
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
	}
	
	@Test
	public void testDelete() {
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id=?";
			statement = connection.prepareStatement(sql);
			// 填上？具体的值
			statement.setInt(1, 8);
			// 返回影响的行数
			int count = statement.executeUpdate();
			System.out.println(statement);
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
	}
	
	@Test
	public void testUpdate() {
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "update student set name=?,age=?,gender=? where id=?";
			statement = connection.prepareStatement(sql);
			statement.setString(1, "张某某");
			statement.setInt(2, 24);
			statement.setString(3, "女");
			statement.setInt(4, 7);
			
			int count = statement.executeUpdate();
			System.out.println(statement);
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
	}
	
	
	
	
	
}
